package com.ywd.materials.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywd.materials.admin.common.Common;
import com.ywd.materials.admin.common.CommonGoodsType;
import com.ywd.materials.admin.common.CommonGoodsTypeOne;
import com.ywd.materials.admin.common.CommonGoodsTypeTwo;
import com.ywd.materials.admin.context.UserContext;
import com.ywd.materials.admin.dto.MaterialsGoodsTypeDto;
import com.ywd.materials.admin.dto.query.MaterialsGoodsTypeQuery;
import com.ywd.materials.admin.entity.MaterialsGoodsEntity;
import com.ywd.materials.admin.entity.MaterialsGoodsTypeEntity;
import com.ywd.materials.admin.exception.CommonException;
import com.ywd.materials.admin.exception.RPage;
import com.ywd.materials.admin.mapper.MaterialsGoodsTypeMapper;
import com.ywd.materials.admin.vo.AdminUserVo;
import com.ywd.materials.admin.vo.MaterialsGoodsTypeVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
 * YWD
 * 2025/11/14
 */
@AllArgsConstructor
@Service
public class MaterialsGoodsTypeServiceImpl extends ServiceImpl<MaterialsGoodsTypeMapper, MaterialsGoodsTypeEntity> {

    private final MaterialsGoodsServiceImpl materialsGoodsServiceImpl;


    public Boolean saveGoodsType(MaterialsGoodsTypeDto dto) {
        AdminUserVo user = UserContext.getUserOne();
        dto.setCreator(user.getId());
        dto.setType1(dto.getName());
        return save(BeanUtil.copyProperties(dto, MaterialsGoodsTypeEntity.class));
    }

    public Boolean updateGoodsType(MaterialsGoodsTypeDto dto) {
        AdminUserVo user = UserContext.getUserOne();
        dto.setUpdater(user.getId());
        dto.setType1(dto.getName());
        return updateById(BeanUtil.copyProperties(dto, MaterialsGoodsTypeEntity.class));
    }

    public Boolean deleteGoodsType(Long id) {
        //是否正在使用
        List<MaterialsGoodsEntity> list = materialsGoodsServiceImpl.getByGoodsTypeId(id);
        if (CollectionUtil.isNotEmpty(list)) throw new CommonException("该物资类型正在使用, 不能删除");
        return removeById(id);
    }

    public RPage<List<MaterialsGoodsTypeVo>> getGoodsTypePage(MaterialsGoodsTypeQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPage(), query.getLimit());
        LambdaQueryWrapper<MaterialsGoodsTypeEntity> queryWrapper = Wrappers.lambdaQuery(MaterialsGoodsTypeEntity.class);
        queryWrapper.eq(MaterialsGoodsTypeEntity::getDeleted, 0)
                .like(ObjectUtil.isNotEmpty(query.getName()), MaterialsGoodsTypeEntity::getName, query.getName())
                .like(ObjectUtil.isNotEmpty(query.getType1()), MaterialsGoodsTypeEntity::getType1, query.getType1())
                .like(ObjectUtil.isNotEmpty(query.getType2()), MaterialsGoodsTypeEntity::getType2, query.getType2())
                .like(ObjectUtil.isNotEmpty(query.getType3()), MaterialsGoodsTypeEntity::getType3, query.getType3())
                .eq(ObjectUtil.isNotEmpty(query.getStatus()), MaterialsGoodsTypeEntity::getStatus, query.getStatus());
        List<MaterialsGoodsTypeEntity> list = list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) return null;
        return RPage.ok(BeanUtil.copyToList(list, MaterialsGoodsTypeVo.class), query.getPage(), query.getLimit(), page.getTotal());
    }


    public List<String> getGoodsTypeLevelList(Integer level, String name) {
        List<String> list = new ArrayList<>();
        if (level.equals(2)) {
            List<CommonGoodsTypeOne> typeOnes = this.baseMapper.getTypeOnes(null, name);
            list = typeOnes.stream().map(CommonGoodsTypeOne::getName).toList();
        } else if (level.equals(3)) {
            List<CommonGoodsTypeTwo> typeTwos = this.baseMapper.getTypeTwos(null, name);
            list = typeTwos.stream().map(CommonGoodsTypeTwo::getName).toList();
        } else {
            List<CommonGoodsType> list1 = this.baseMapper.getGoodsTypePartOneList(name);
            list = list1.stream().map(CommonGoodsType::getName).toList();
        }
        return list;
    }

    public List<Common> getGoodsTypePartList(String name) {
        return this.baseMapper.getGoodsTypePartList(name);
    }

    public List<CommonGoodsType> getGoodsTypePartOneList() {
        List<CommonGoodsType> list = this.baseMapper.getGoodsTypePartOneList(null);
        if (CollectionUtil.isEmpty(list)) return null;
        AtomicLong i = new AtomicLong(1000L);
        list.forEach(it -> {
            it.setId(Long.valueOf(i.getAndIncrement()));
            List<CommonGoodsTypeOne> typeOnes = this.baseMapper.getTypeOnes(it.getName(), null);
            if(CollectionUtil.isNotEmpty(typeOnes)){
                typeOnes.forEach(it1 -> {
                    if(ObjectUtil.isNotEmpty(it1)){
                        AtomicLong j = new AtomicLong(100L + i.getAndIncrement());
                        it1.setId(j.getAndIncrement());
                        List<CommonGoodsTypeTwo> typeTwos = this.baseMapper.getTypeTwos(it1.getName(), null);
                        if(CollectionUtil.isNotEmpty(typeTwos)){
                            it1.setChildren(typeTwos);
                        }else {
                            it1.setChildren(null);
                        }
                    }
                });
                it.setChildren(typeOnes);
            }else  {
                it.setChildren(null);
            }
        });
        return list;
    }


}
